package com.glebusheg.data
{
	import com.glebusheg.debug.Console;
	import com.glebusheg.text.StringManager;
	/**
	 * @author eliasku
	 */
	public class MarketData
	{
		private var _id:String;
		private var _title:String = "";
		private var _tabs:Vector.<ShopGroupData> = new Vector.<ShopGroupData>();
		
		// private var _dialogDef:String = "mc_shop_dialog";
		// private var _itemDef:String = "mc_shop_item";

		public function MarketData()
		{
		}
		
		public function get id():String
		{
			return _id;
		}
		
		public function get tabs():Vector.<ShopGroupData>
		{
			return _tabs;
		}

		public function get title():String
		{
			return _title;
		}
		
		public function parse(xml:XML):void
		{
			var node:XML;
			var group:ShopGroupData;
			
			if(xml)
			{
				if(xml.hasOwnProperty("@id"))
					_id = xml.@id;
					
				if(xml.hasOwnProperty("@title"))
					_title = StringManager.localize(xml.@title);
					
/*				if(xml.hasOwnProperty("@dialog"))
					_dialogDef = xml.@dialog;
					
				if(xml.hasOwnProperty("@item"))
					_itemDef = xml.@item;*/
				
				for each (node in xml.tab)
				{
					if (node.hasOwnProperty("@group"))
					{
						group = getGroup(node.@group);
						if (group)
							_tabs.push(group);
					}
				}
			}
		}

		private static var _groupsLookup:Object = new Object();
		private static var _groups:Vector.<ShopGroupData> = new Vector.<ShopGroupData>();
		
		private static var _marketsLookup:Object = new Object();
				
		public static function parse(xml:XML):void
		{
			var group:ShopGroupData;
			var market:MarketData;
			var node:XML;
			var sub:XML;
			
			if(xml)
			{
				sub = xml.groups[0];
				if(sub)
				{
					for each (node in sub.group)
					{
						group = new ShopGroupData();
						group.parse(node);
						if(group.id)
						{
							_groupsLookup[group.id] = group;
							_groups.push(group);
						}
					}
				}
				
				sub = xml.markets[0];
				if(sub)
				{
					for each (node in sub.market)
					{
						market = new MarketData();
						market.parse(node);
						if(market.id)
							_marketsLookup[market.id] = market;
					}
				}
			}
		}
		
		public static function getGroup(id:String):ShopGroupData
		{
			if(id && _groupsLookup.hasOwnProperty(id))
				return _groupsLookup[id];
				
			Console.warn("[Markets] Group '" + id + "' not found");
			
			return null;
		}

		static public function getMarket(id:String):MarketData
		{
			if(id && _marketsLookup.hasOwnProperty(id))
				return _marketsLookup[id];
				
			Console.warn("[Markets] Market '" + id + "' not found");
			
			return null;
		}

/*		public function get dialogDef():String
		{
			return _dialogDef;
		}

		public function get itemDef():String
		{
			return _itemDef;
		}*/
	}
}
